package com.chookss.tiku;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chookss.tools.MyToast;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionFragment;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class TikuFragments extends SimpleImmersionFragment {

    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_back)
    LinearLayout title_back;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.layoutTop)
    RelativeLayout layoutTop;
    @BindView(R.id.tvSmart)
    TextView tvSmart;
    @BindView(R.id.tvSimulate)
    TextView tvSimulate;
    @BindView(R.id.tvSpecially)
    TextView tvSpecially;
    @BindView(R.id.tvError)
    TextView tvError;
    @BindView(R.id.tvCollection)
    TextView tvCollection;
    private Activity mContext;
    Unbinder unbinder;
    int errorNum = 0, collectNum = 0;
    private int type = 0; //0一级页面没有返回1是activity要返回

    public TikuFragments() {

    }

    public TikuFragments(int type) {
        this.type = type;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tikus, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        //顶部状态栏 处理
        NavigationTools.saveUseRecord(mContext,"1","13");
        if (type == 0) {
            int statusBarHeight = Utils.getStatusBarHeight(mContext);
            RelativeLayout.LayoutParams topAllParams = (RelativeLayout.LayoutParams) statusBar.getLayoutParams();
            topAllParams.height = statusBarHeight;
            statusBar.setLayoutParams(topAllParams);
            statusBar.setVisibility(View.VISIBLE);
            title_back.setVisibility(View.GONE);
            tvTitle.getPaint().setFakeBoldText(true);
        } else {
            statusBar.setVisibility(View.GONE);
            title_back.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        getData();
    }

    private void getData() {
        MyHttpRequest.postRequest(Urls.getSubjectBusinessNum, this, SignUtils2.makeSignStr(null), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (tvSmart == null) {
                            return;
                        }
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            String type = jsonObject1.getString("type");
                            String num = jsonObject1.getString("num");
                            if ("01".equals(type)) {
                                tvSmart.setText("收录" + num + "道");
                            } else if ("02".equals(type)) {
                                tvSpecially.setText("共有" + num + "个专项");
                            } else if ("03".equals(type)) {
                                tvSimulate.setText("收录" + num + "套");
                            } else if ("04".equals(type)) {
                                tvError.setText("共有" + num + "道错题");
                                errorNum = Utils.changeNumber(num);
                            } else if ("05".equals(type)) {
                                tvCollection.setText("共有" + num + "道收藏");
                                collectNum = Utils.changeNumber(num);
                            }
                        }

                    }else{
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void initImmersionBar() {
        if (getActivity() == null) {
            return;
        }
        ImmersionBar.with(getActivity())
                .transparentStatusBar()  /*透明状态栏，不写默认透明色*/  /*状态栏字体是深色，不写默认为亮色*/
                .statusBarDarkFont(true)
                .flymeOSStatusBarFontColor(R.color.grey_3)  /*修改flyme OS状态栏字体颜色*/
                .keyboardEnable(false).navigationBarEnable(false)
                .init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @OnClick({R.id.title_back, R.id.ivSearch, R.id.llSmart, R.id.llSimulate, R.id.llSpecially, R.id.rlError, R.id.rlCollection, R.id.rlRecord, R.id.llKnowlege})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.title_back:
                if(mContext!=null){
                    mContext.finish();
                }
                break;
            case R.id.ivSearch:
                intent = new Intent(mContext, SearchSubjectActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.llSmart:
                intent = new Intent(mContext, PracticeTestActivity.class);
//                //练习类型 1智能题库 2 专项练习
                intent.putExtra("practiceType", "1");
                mContext.startActivity(intent);
                break;
            case R.id.llSimulate:
                //模拟卷
                intent = new Intent(mContext, SelectSimulationCatalog.class);
                mContext.startActivity(intent);
                break;
            case R.id.llSpecially:
                //专项练习
                intent = new Intent(mContext, SelectPracticeCatalog.class);
                mContext.startActivity(intent);
                break;
            case R.id.rlError:
                //错题本
                intent = new Intent(mContext, MyErrorSubjectActivity.class);
                intent.putExtra("subjectAllNums", errorNum);
                mContext. startActivity(intent);
                break;
            case R.id.rlCollection:
                //我的收藏
                intent = new Intent(mContext, MyCollectSubjectActivity.class);
                intent.putExtra("subjectAllNums", collectNum);
                mContext.startActivity(intent);
                break;
            case R.id.rlRecord:
                //答题记录
                intent = new Intent(mContext, ExamRecordListActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.llKnowlege:
                //知识构成
                intent = new Intent(mContext, WebCommonActivity.class);
                intent.putExtra("url", NavigationTools.getAllUrl(mContext, Urls.getKnowcomposition));
                mContext.startActivity(intent);
                break;
        }
    }

}
